﻿using UnityEngine;
using System.Collections;
using System.Linq;

public class PortraitControl : MonoBehaviour {

    static string prefabPath = "Prefab/Controllers/PortraitController";
    static PortraitControl _instance = null;
    
    UISprite[] m_AtlasContainer = new UISprite[(int)Portrait.max];

	public UIAtlas[] portraitCharacterAtlas;
	public UIAtlas[] portraitBattleAtlas;

    public static  PortraitControl Instance
    {
        get {
            if (_instance == null)
                Create();            
            return _instance; }
        set { _instance = value; }
    }

    static void Create()
    {
        GameObject preFab = ResourceLoader.Load(prefabPath) as GameObject;
        GameObject temp = Instantiate(preFab) as GameObject;
        DontDestroyOnLoad(temp);
		_instance = temp.GetComponent("PortraitControl") as PortraitControl;
    }

    void Awake()
    {
        int max = (int)Portrait.max;
        for (int i = 0; i < max ; i++) 
        {
            m_AtlasContainer[i] = transform.Find(((Portrait)i).ToString()).GetComponent("UISprite") as UISprite;
        }
    }

    public static void SetPortrait(UISprite sprite, MemberModel model)
    {
        SetPortrait(sprite, model, true);
    }


    public static void SetPortrait(UISprite sprite, MemberModel model, bool isOn)
    {
        string modelName = GameData.Inst.ModelDefineDic[model.model];

        if (!isOn)
            modelName = string.Format("{0}_off", modelName);

        SetPortrait(sprite, modelName);
    }

    public static void SetPortrait(UISprite sprite, string modelName)
    {        
        string ModelClass = MyUtil.FindClassWithModelName(modelName);
	
        int max = (int)Portrait.max;
        int portraitIndex = 0;  //기타등등은 0
        for (int i = 1; i < max ; i++)  // 1번부터 검사
        {
            if((((Portrait)i).ToString()).Contains(ModelClass))
            {
                portraitIndex = i;
                break;
            }
        }

        sprite.atlas = Instance.m_AtlasContainer[portraitIndex].atlas;
        sprite.spriteName = modelName;
    }

	/// <summary>
	/// 모델명으로 전투시 사용하는 초상화 초기화.
	/// </summary>
	/// <param name="sprite"></param>
	/// <param name="modelName"></param>
	public static void SetBattlePortrait(UISprite sprite, string modelName)
	{
		UIAtlas atlas;
       
        string className = MyUtil.FindClassWithModelName(modelName);
		
        Instance.FindBattlePortrait(className, out atlas);

		sprite.atlas = atlas;
		sprite.spriteName = string.Format("Battle_{0}", modelName);
	}

	/// <summary>
	/// 클래스명으로 전투시 사용하는 초상화 초기화.
	/// </summary>
	/// <param name="sprite"></param>
	/// <param name="className"></param>
	public static void SetBattlePortraitWithClass(UISprite sprite, string className)
	{
		UIAtlas atlas;
		string spriteName;

		Instance.FindBattlePortrait(className, out atlas, out spriteName);

		sprite.atlas = atlas;
		sprite.spriteName = spriteName;
	}

	private void FindBattlePortrait(string className, out UIAtlas atlas)
	{
		if ((atlas = portraitCharacterAtlas.FirstOrDefault(data => data.name.EndsWith(className))) == null)
			atlas = portraitCharacterAtlas.FirstOrDefault();
	}
	
	private void FindBattlePortrait(string className, out UIAtlas atlas, out string spriteName)
	{
		spriteName = null;

		FindBattlePortrait(className, out atlas);

		if (atlas == null)
			return;

		var sprite = atlas.spriteList.FirstOrDefault(data => data.name.Contains(className));

		if (sprite == null)
		{
			if ((sprite = atlas.spriteList.FirstOrDefault()) == null)
				return;
		}

		spriteName = sprite.name;
	}
}
